package com.ujoku.controller;

import com.labillusion.core.platform.exception.ResourceNotFoundException;
import com.labillusion.core.platform.web.rest.RESTController;
import com.ujoku.domain.Article;
import com.ujoku.service.ArticleService;
import com.ujoku.view.domain.ArticleView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;


/**
 * Created by greg.chen on 14-10-21.
 */
@RestController
public class ArticleController extends RESTController {

    @Autowired
    public ArticleService service;

    @RequestMapping(value="/article/{id}", method = RequestMethod.GET)
    @ResponseBody
    public ArticleView getArticleById(@PathVariable int id){
        ArticleView view = new ArticleView();
        Article article = service.selectById(id);
        if(article == null)
            throw new ResourceNotFoundException("can not found article by id.");

        Article pre = service.selectById(id - 1);
        if(pre != null)
            pre.setContent(null);
        Article next = service.selectById(id + 1);
        if(next != null)
        next.setContent(null);

        view.setNext(next);
        view.setCurrent(article);
        view.setPrevious(pre);
        return view;
    }
}
